﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using matrixdotnet.CommonDAL;
using matrixdotnet.DBHelperLib;
using matrixdotnet.Common;
using matrixdotnet.CommonDALImp;

namespace Cinema.DAL
{
    public interface IFilmFinder : IBusinessEntityFinder<FilmUniqueKey, IFilm>
    {
        void FindStartFrom(string startFrom, ICollection<IFilm> films);
    }

    /*
    public interface IFilmDataEntityFinder : IDataEntityFinderL<FilmUniqueKey, IFilm, IFilmDataEntity>
    {
    }

    public interface IFromFilmObjectMapper : IFromDataObjectMapperL<FilmUniqueKey, IFilm, IFilmDataEntity>
    {
    }

    public interface IToFilmMapper : IToBusinessObjectMapperL<FilmUniqueKey, IFilm>
    {
    }
     * */

    public class FilmDataMapper :
        DataMapperL<FilmUniqueKey, IFilm, FilmDataEntity, FilmLazyEntity>,
        IFilmFinder,
        IDataEntityFinderL<FilmUniqueKey, IFilm, IFilmDataEntity>,
        IFromDataObjectMapperL<FilmUniqueKey, IFilm, IFilmDataEntity>,
        IToBusinessObjectMapperL<FilmUniqueKey, IFilm>
    {
        protected IFilmDataEntityFactory FilmDataEntityFactory { get; set; }

        public FilmDataMapper(IFilmDataEntityFactory factory, DBHelper helper) : base(helper)
        {
            FilmDataEntityFactory = factory;

            // FilmDataEntityFactory.DataMapper = this;
        }

        protected override int UpdateEntity(IFilmDataEntity entity)
        {
            throw new NotImplementedException();
        }

        protected override void SetUniqueKeyParameters(FilmUniqueKey uniqueKey)
        {
            throw new NotImplementedException();
        }

        protected override void SetBusinessEntityParameters(IFilm businessEntity)
        {
            throw new NotImplementedException();
        }

        protected override void SetVersionParameter(long version)
        {
            throw new NotImplementedException();
        }

        protected override DbDataReader ExecuteFindByIdentityReader()
        {
            throw new NotImplementedException();
        }

        protected override DbDataReader ExecuteFindByUniqueKeyReader()
        {
            throw new NotImplementedException();
        }

        protected override int ExecuteUpdate()
        {
            throw new NotImplementedException();
        }

        protected override long ExecuteInsert()
        {
            throw new NotImplementedException();
        }

        public void FindStartFrom(string findFrom, ICollection<IFilm> films)
        {
            DbHelper.Command.Parameters.Add(DbHelper.CreateParameter("@FilmNameStartFrom", findFrom));
            ToFilmMapper mapper = CreateToFilmMapper();
            mapper.MapMany(DbHelper.ExecuteSPReader("FindFilmsStartFrom"), films);
        }

        protected override IDataEntityFactory<long, FilmUniqueKey, IFilm, IFilmDataEntity, FilmLazyEntity> DataEntityFactory
        {
            get { return FilmDataEntityFactory; }
        }

        protected override ToDataObjectMapperL CreateToDataObjectMapperL()
        {
            return CreateToFilmMapper();
        }

        protected override string VersionColumnName
        {
            get { return "FilmVersion"; }
        }

        private ToFilmMapper CreateToFilmMapper()
        {
            return new ToFilmMapper(this);
        }

        protected class ToFilmMapper : ToDataObjectMapperL
        {
            private FilmDataMapper _filmDataMapper;
            private int _filmNameOrd = -1;
            private int _filmDirectorOrd = -1;
            private int _filmDescriptionOrd = -1;
            private int _filmImagePathOrd = -1;
            private int _filmDurationOrd = -1;
            private int _filmDiscontinueDateOrd = -1;

            public ToFilmMapper(FilmDataMapper filmDataMapper)
            {
                _filmDataMapper = filmDataMapper;
            }

            protected override DataMapperL<FilmUniqueKey, IFilm, IFilmDataEntity, FilmLazyEntity> LDataMapper
            {
                get { return _filmDataMapper; }
            }

            protected override void SetBusinessOrdinals(DbDataReader reader)
            {
                _filmNameOrd = reader.GetOrdinal("FilmName");
                _filmDirectorOrd = reader.GetOrdinal("FilmDirector");
                _filmDescriptionOrd = reader.GetOrdinal("FilmDescription");
                _filmImagePathOrd = reader.GetOrdinal("FilmImagePath");
                _filmDurationOrd = reader.GetOrdinal("FilmDuration");
                _filmDiscontinueDateOrd = reader.GetOrdinal("FilmDiscontinueDate");
            }

            protected override IFilm MapBusinessEntity(DbDataReader reader)
            {
                return _filmDataMapper.FilmDataEntityFactory.Create(
                    reader.GetString(_filmNameOrd),
                    reader.GetString(_filmDescriptionOrd),
                    reader.GetString(_filmDirectorOrd),
                    reader.GetString(_filmImagePathOrd),
                    new PositiveValue<short>(reader.GetInt16(_filmDurationOrd)),
                    reader.IsDBNull(_filmDiscontinueDateOrd) ?
                        null : (DateTime?)reader.GetDateTime(_filmDiscontinueDateOrd));
            }
        }

        protected override string IdentityColumnName
        {
            get { return "FilmIdentity"; }
        }
    }
}
